﻿using Gui.MyUserControl;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Gui.Pages
{
    /// <summary>
    /// Page1.xaml 的交互逻辑
    /// </summary>
    public partial class Page1 : Page
    {
        Page1ViewModel vm = new Page1ViewModel();
        private readonly ShowMsgDelegate ShowMsg;
        public Page1(ShowMsgDelegate showMsg)
        {
            InitializeComponent();
            DataContext = vm;
            this.ShowMsg = showMsg;
            var lang = System.Windows.Markup.XmlLanguage.GetLanguage("zh-CN");
            localDatePicker.Language= localTimePicker.Language = lang;

            vm.Materials.Add(new MaterialType() { ID = 1, Name = "酒瓶" });
            vm.Materials.Add(new MaterialType() { ID = 2, Name = "瓶盖" });

            vm.Units.Add(new Unit() { ID = 1, Name = "捆" });
            vm.Units.Add(new Unit() { ID = 2, Name = "盒" });

            vm.Repositories.Add(new Repository() { ID = 1, Name = "一号仓" });
            vm.Repositories.Add(new Repository() { ID = 2, Name = "二号仓" });

            vm.Suppliers.Add(new Supplier() { ID = 1, Name = "供应商A" });
            vm.Suppliers.Add(new Supplier() { ID = 2, Name = "供应商B" });

            vm.Items.Add(new Item() { Code = "aaaaaaaaaaaa", Count = "101", Material = 1, Repo = 2, Supplier = 1, Unit = 2, Date=DateTime.Now, Time=DateTime.Now });
            vm.Items.Add(new Item() { Code = "bbbbbbbbbbbb", Count = "102", Material = 2, Repo = 2, Supplier = 2, Unit = 2 });
            vm.Items.Add(new Item() { Code = "cccccccccccc", Count = "103", Material = 1, Repo = 1, Supplier = 1, Unit = 1 });
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            Scan.Scanner.AddValidCodeHandler(OnIndustryCodeScan);
            Scan.Scanner.AddInValidCodeHandler(onInvalidCodeScan);
            Console.WriteLine("p1 load");

        }

        private void Page_Unloaded(object sender, RoutedEventArgs e)
        {
            Scan.Scanner.RemoveValidCodeHandler(OnIndustryCodeScan);
            Scan.Scanner.RemoveInValidCodeHandler(onInvalidCodeScan);
            Console.WriteLine("p1 unload");
        }

        private void onInvalidCodeScan(string invalidCode)
        {
            Dispatcher.Invoke(() =>
            {
                ShowMsg("errcode:" + invalidCode);
            });
            
        }

        private void OnIndustryCodeScan(string codes)
        {
            Console.WriteLine(codes+Thread.CurrentThread.Name);
            vm.Current.Code = codes;
            vm.OpenDialog(null);
        }

        private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[0-9]");
            var match = re.IsMatch(e.Text);
            e.Handled = !match;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine(Thread.CurrentThread.Name);
            var sss = vm.Current;
            vm.OpenDialog(null);
            ShowMsg("OPEN");
        }

        private void Pager_PageChange(object sender, RoutedEventArgs e)
        {
            var arg = (PageChangeArgs)e;
            ShowMsg(arg.PageNo+" "+arg.PageSize);
            vm.CurrentPage = arg.PageNo;
            vm.TotalPage = 199;
        }

    }

    class Page1ViewModel:NotifyBase
    {
        
        internal Page1ViewModel()
        {
            Current = new Item();
            CloseDialogCommand = new CommandImpl(CloseDialog);
            OpenDialogCommand = new CommandImpl(OpenDialog);
            Materials = new ObservableCollection<MaterialType>();
            Units = new ObservableCollection<Unit>();
            Repositories = new ObservableCollection<Repository>();
            Suppliers = new ObservableCollection<Supplier>();
            Items = new ObservableCollection<Item>();
            CurrentPage = 100;
            TotalPage = 109;
            
        }

        public void OpenDialog(object param)
        {
            ShowDialog = true;
        }

        private void CloseDialog(object param)
        {
            ShowDialog = false;
        }

        public ICommand CloseDialogCommand { get; }
        public ICommand OpenDialogCommand { get; }

        public ObservableCollection<MaterialType> Materials { get; set; }
        public ObservableCollection<Unit> Units { get; set; }
        public ObservableCollection<Repository> Repositories { get; set; }
        public ObservableCollection<Supplier> Suppliers { get; set; }
        public Item Current
        {
            get { return _currentItem; }
            set
            {
                _currentItem = value;
                RaisePropertyChanged("Current");
            }
        }

        public ObservableCollection<Item> Items { get; set; }

        public int CurrentPage
        {
            get { return _currentPage; }
            set
            {
                _currentPage = value;
                RaisePropertyChanged("CurrentPage");
            }
        }

        public int TotalPage
        {
            get { return _totalPage; }
            set
            {
                _totalPage = value;
                RaisePropertyChanged("TotalPage");
            }
        }

        public bool ShowDialog
        {
            get { return _showDialog; }
            set
            {
                _showDialog = value;
                RaisePropertyChanged("ShowDialog");
            }
        }
        
        private bool _showDialog;
        private Item _currentItem;
        private int _totalPage;
        private int _currentPage;
    }

    class Item:NotifyBase
    {
        public string Code
        {
            get { return _code; }
            set
            {
                _code = value;
                RaisePropertyChanged("Code");
            }
        }

        public string Count
        {
            get { return _count; }
            set
            {
                _count = value;
                RaisePropertyChanged("Count");
            }
        }

        public int Material
        {
            get { return _material; }
            set {
                _material = value;
                RaisePropertyChanged("Material");
            }
        }

        public int Unit
        {
            get { return _unit; }
            set
            {
                _unit = value;
                RaisePropertyChanged("Unit");
            }
        }

        public int Repo
        {
            get { return _repo; }
            set
            {
                _repo = value;
                RaisePropertyChanged("Repo");
            }
        }

        public int Supplier
        {
            get { return _supplier; }
            set
            {
                _supplier = value;
                RaisePropertyChanged("Supplier");
            }
        }

        public DateTime Date
        {
            get
            {
                return _date;
            }
            set
            {
                _date = value;
                RaisePropertyChanged("Date");
            }
        }
        public DateTime Time
        {
            get
            {
                return _time;
            }
            set
            {
                _time = value;
                RaisePropertyChanged("Time");
            }
        }

        private string _count;
        private int _material;
        private int _unit;
        private int _repo;
        private int _supplier;
        private string _code;
        private DateTime _date;
        private DateTime _time;
    }

    class MaterialType
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    class Unit
    {
        public int ID { get; set; }
        public string Name {get; set; }
}

    class Repository
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }

    class Supplier
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}
